package com.kgc.warehouse.bussness.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.warehouse.bussness.entity.Goods;
import com.kgc.warehouse.bussness.entity.Outport;
import com.kgc.warehouse.bussness.entity.Provider;
import com.kgc.warehouse.bussness.service.GoodsService;
import com.kgc.warehouse.bussness.service.OutportService;
import com.kgc.warehouse.bussness.service.ProviderService;
import com.kgc.warehouse.bussness.vo.OutportVO;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@RestController()
@RequestMapping("outport")
public class OutportController {
    @Resource
    private OutportService outportService;

    @Resource
    private ProviderService providerService;

    @Resource
    private GoodsService goodsService;

    @RequestMapping("addOutport")
    public ResponseResult addOutport(Integer id, Integer number, String remark, HttpSession session){
        try {
            return outportService.addPort(id,number,remark,session);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.BACKINPORT_FAIL);
        }
    }

    @RequestMapping("addOutports")
    public ResponseResult addOutports(Integer id, Integer number, String remark, HttpSession session){
        try {
            return outportService.addPorts(id,number,remark,session);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.BACKINPORT_FAIL);
        }
    }

    @RequestMapping("loadAllOutport")
    public ResponseResult loadAllOutport(OutportVO outportVO){
        Page<Outport> outportPage = new Page<>(outportVO.getPage(),outportVO.getLimit());
        LambdaQueryWrapper<Outport> outportLambdaQueryWrapper = new LambdaQueryWrapper<>();
        outportLambdaQueryWrapper.eq(outportVO.getProviderid()!=null&&outportVO.getProviderid()!=0,Outport::getProviderid,outportVO.getProviderid());
        outportLambdaQueryWrapper.eq(outportVO.getGoodsid()!=null&&outportVO.getGoodsid()!=0,Outport::getGoodsid,outportVO.getGoodsid());
        outportLambdaQueryWrapper.ge(outportVO.getStartTime()!=null,Outport::getOutputtime,outportVO.getStartTime());
        outportLambdaQueryWrapper.le(outportVO.getEndTime()!=null,Outport::getOutputtime,outportVO.getEndTime());
        outportLambdaQueryWrapper.orderByDesc(Outport::getOutputtime);
        Page<Outport> page = outportService.page(outportPage, outportLambdaQueryWrapper);
        List<Outport> records = page.getRecords();
        long total = page.getTotal();
        List<Outport> collect = records.stream().map(record -> {
            Provider provider = providerService.getById(record.getProviderid());
            if (ObjectUtils.isNotNull(provider)) {
                //设置供应商名
                record.setProvidername(provider.getProvidername());
            }
            Goods goods = goodsService.getById(record.getGoodsid());
            if (ObjectUtils.isNotNull(goods)) {
                //设置商品名
                record.setGoodsname(goods.getGoodsname());
                //设置商品规格
                record.setSize(goods.getSize());
            }

            return record;
        }).collect(Collectors.toList());
        return new ResponseResult(collect,total,CodeMsg.SUCCESS);
    }

    @RequestMapping("deleteOutport")
    public ResponseResult deleteOutport(Integer id){
        try {
            outportService.removeById(id);
            return new ResponseResult(CodeMsg.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.DELETE_FAIL);
        }
    }
}
